#include "stu.h"

Book_type_index book_type_index[TYPE_MAX];
Book_key_index book_key_index[KEY_MAX];
Student* student;
char id[8];

void menu(void)
{
    system("clear");
    puts("*****学生图书馆*****");
	puts("1、预约");
	puts("2、查看借书记录");
    puts("3、查看归还记录");
    puts("4、查看预约记录");
    puts("5、查看逾期记录");
	puts("6、退出系统");
}

void menu_order(void)
{
    system("clear");
    puts("*****预约*****");
    puts("1、按首字母搜索");
	puts("2、按类型搜索");
    puts("3、模糊查找");
	puts("4、退出预约");
}


int login_stu(void)  //学生登录
{
	system("clear");
	printf("请输入学生ID\n");
	get_str(id,sizeof(id));
	printf("请输入密码\n");
	char password[20];
	get_passwd(password,sizeof(password));
    student = read_stu(id);
    if(NULL == student ||0 != strcmp(password,student->password))
    {
        printf("密码输入错误!\n");
        return -1;
    }
    return 0;
}

void stu(void)
{
    if(0 != login_stu())
    {
        return;
    }
    for(;;)
	{
		menu();
		switch(get_cmd('1','6'))
		{
		case '1': order();  	    break;
		case '2': show_borrow();    break;
        case '3': show_repay();		break;
        case '4': show_order();		break;
        case '5': show_overdue();	break;
		case '6': return;
		}
	}
}

void order(void)
{
    for(;;)
	{
		menu_order();
		switch(get_cmd('1','4'))
		{
		case '1': find_key();  	    break;
		case '2': find_type();      break;
        case '3': find_fuzzy();		break;
		case '4': return;
		}
	}
}

void find_key(void)
{
    load_book_index();
    system("clear");
    printf("请输入书名首字母:\n");
    char key[2];
    size_t key_type = 0;
    while(1)
    {
        get_str(key,sizeof(key));
        if(strlen(key) > 1)
        {
            printf("输入了过多位数");
            continue;
        }
        if(*key>='A' && *key<='Z')
        {
            key_type = *key-'A';
            break;
        }
        if(*key>='a' && *key<='z')
        {
            key_type = *key-'a';
            break;
        }
        printf("首写字母输入不符合规范,请重新输入!(a~z 或 A~Z)\n");
    }
    if(0 == book_key_index[key_type].sum)
    {
        print_sec("该字母下没有相应图书!\n",0.75);
        return;
    }
    for(int i=0;i<book_key_index[key_type].sum;i++)
    {
        Book* book = read_book_msg(book_key_index[key_type].id[i]);
        printf("书名:%s 书号:%s 作者:%s 出版社:%s\n",book->bookname,book->ISBN,book->writer,book->publish);
        printf("总数:%u 价格%.2f 类型:%u 首字母:%u\n",book->book_sum,book->book_price,book->type,book->key);
        printf("出借:%u 预约:%u 自然损坏:%u 人为损坏:%u\n\n",book->book_borrowed,book->book_ordered,book->broken_aging,book->broken_manmade);
        free(book);
    }
    printf("请输入您要预约的书号:\n");
    char ISBN[14];
    get_str(ISBN,sizeof(ISBN));
    Book* book = read_book_msg(ISBN);
    if(NULL == book)
    {
        print_sec("没有这本书!\n",0.75);
        return;
    }
    printf("书名:%s 作者:%s 出版社:%s\n",book->bookname,book->writer,book->publish);
    printf("总数:%u 价格%.2f 类型:%u 首字母:%u\n",book->book_sum,book->book_price,book->type,book->key);
    printf("出借:%u 预约:%u 自然损坏:%u 人为损坏:%u\n\n",book->book_borrowed,book->book_ordered,book->broken_aging,book->broken_manmade);
    printf("确定预约?\n");
    if(yes_or_no())
    {
        book->book_ordered++;
        student->recond->order_sum++;
        time_t current_time;
        add_node_recond(student->recond->head_order,book->ISBN,time(&current_time));
        print_sec("预约成功!",0.75);
    }
    write_book_msg(book);
    write_stu(student);
    free(book);
}

void find_type(void)
{
    load_book_index();
    system("clear");
    printf("请输入书籍类别:\n");
    puts("1.马克思主义");
    puts("2.哲学、宗教");
    puts("3.社会科学总论");
    puts("4.政治、法律");
    puts("5.经济");
    puts("6.文化、科学、教育、体育");
    puts("7.文学");
    puts("8.艺术");
    puts("9.历史、地理");
    puts("0.自然科学");
    size_t type = 0;
    switch(get_cmd('0','9'))
	{
        case '0':type = 0     ;break;
		case '1':type = 1     ;break;                         
		case '2':type = 2     ;break;                     
        case '3':type = 3     ;break;
        case '4':type = 4     ;break;
		case '5':type = 5     ;break;
        case '6':type = 6     ;break;
        case '7':type = 7     ;break;
        case '8':type = 8     ;break;
        case '9':type = 9     ;break;
	}
    if(0 == book_type_index[type].sum)
    {
        print_sec("该类型下没有相应图书!\n",0.75);
        return;
    }
    for(int i=0;i<book_type_index[type].sum;i++)
    {
        Book* book = read_book_msg(book_type_index[type].id[i]);
        printf("书名:%s 书号:%s 作者:%s 出版社:%s\n",book->bookname,book->ISBN,book->writer,book->publish);
        printf("总数:%u 价格%.2f 类型:%u 首字母:%u\n",book->book_sum,book->book_price,book->type,book->key);
        printf("出借:%u 预约:%u\n\n",book->book_borrowed,book->book_ordered);
        free(book);
    }
    printf("请输入您要预约的书号:\n");
    char ISBN[14];
    get_str(ISBN,sizeof(ISBN));
    Book* book = read_book_msg(ISBN);
    if(NULL == book)
    {
        return;
    }
    printf("书名:%s 作者:%s 出版社:%s\n",book->bookname,book->writer,book->publish);
    printf("总数:%u 价格%.2f 类型:%u 首字母:%u\n",book->book_sum,book->book_price,book->type,book->key);
    printf("出借:%u 预约:%u\n\n",book->book_borrowed,book->book_ordered);
    printf("确定预约?\n");
    if(yes_or_no())
    {
        book->book_ordered++;
        student->recond->order_sum++;
        time_t current_time;
        add_node_recond(student->recond->head_order,book->ISBN,time(&current_time));
        print_sec("预约成功!",0.75);
    }
    write_book_msg(book);
    write_stu(student);
    free(book);
}

void find_fuzzy(void)
{
    load_book_index();
    printf("请输入书名:\n");
    char name[100];
    get_str(name,sizeof(name));

    FILE* rfp = fopen("/home/coco/图书馆管理系统/book_data/book_name","r");
    int sum = 0;            //记录找到书籍数量

    char book_name[100],ISBN[14];
    while(2 == fscanf(rfp,"%s %s",book_name,ISBN))
    {
        if(NULL != strstr(book_name,name))
        {
            Book* book = read_book_msg(ISBN);
            sum++;
            printf("书名:%s 书号:%s 作者:%s 出版社:%s\n",book->bookname,book->ISBN,book->writer,book->publish);
            printf("总数:%u 价格%.2f 类型:%u 首字母:%u\n",book->book_sum,book->book_price,book->type,book->key);
            printf("出借:%u 预约:%u\n\n",book->book_borrowed,book->book_ordered);
            free(book);
        }
    }
    fclose(rfp);

    if(0 == sum)
    {
        print_sec("没有对应名字的书籍!\n",0.75);
        return;
    }
    printf("请输入您要预约的书号:\n");
    get_str(ISBN,sizeof(ISBN));
    Book* book = read_book_msg(ISBN);
    if(NULL == book)
    {
        print_sec("没有这本书!\n",0.75);
        return;
    }
    printf("书名:%s 作者:%s 出版社:%s\n",book->bookname,book->writer,book->publish);
    printf("总数:%u 价格%.2f 类型:%u 首字母:%u\n",book->book_sum,book->book_price,book->type,book->key);
    printf("出借:%u 预约:%u\n\n",book->book_borrowed,book->book_ordered);
    printf("确定预约?\n");
    if(yes_or_no())
    {
        book->book_ordered++;
        student->recond->order_sum++;
        time_t current_time;
        add_node_recond(student->recond->head_order,book->ISBN,time(&current_time));
        print_sec("预约成功!",0.75);
    }
    write_book_msg(book);
    write_stu(student);
    free(book);
}

void show_borrow(void)
{
    student = read_stu(id);
    printf("您借了%u本书\n",student->recond->borrow_sum);
    Recond_node* node = student->recond->head_borrow->next;
    for(int i=0;i<student->recond->borrow_sum;i++)
    {
        Book* book = read_book_msg(node->data);
        printf("书名:%s 书号:%s\n",book->bookname,book->ISBN);
        printf("借书时间:");
        show_time_msg(node->time_sec);
        printf("逾期时间:");
        show_time_msg(node->time_sec+BORROW_TIME);
        node = node->next;
        free(book);
    }
    anykey_continue();
}

void show_repay(void)
{
    student = read_stu(id);
    printf("您还了%u本书\n",student->recond->repay_sum);
    Recond_node* node = student->recond->head_repay->next;
    for(int i=0;i<student->recond->repay_sum;i++)
    {
        Book* book = read_book_msg(node->data);
        printf("书名:%s 书号:%s\n",book->bookname,book->ISBN);
        printf("时间:");
        show_time_msg(node->time_sec);
        node = node->next;
        free(book);
    }
    anykey_continue();
}

void show_order(void)
{
    student = read_stu(id);
    printf("您预约了%u本书\n",student->recond->order_sum);
    Recond_node* node = student->recond->head_order->next;
    for(int i=0;i<student->recond->order_sum;i++)
    {
        Book* book = read_book_msg(node->data);
        printf("书名:%s 书号:%s\n",book->bookname,book->ISBN);
        printf("时间:");
        show_time_msg(node->time_sec);
        node = node->next;
        free(book);
    }
    anykey_continue();
}

void show_overdue(void)
{
    student = read_stu(id);
    printf("您逾期了%u本书\n",student->recond->overdue_sum);
    Recond_node* node = student->recond->head_overdue->next;
    for(int i=0;i<student->recond->overdue_sum;i++)
    {
        Book* book = read_book_msg(node->data);
        printf("书名:%s 书号:%s\n",book->bookname,book->ISBN);
        node = node->next;
        free(book);
    }
    anykey_continue();
}